package gcu.module.rbac.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import gcu.common.context.SecurityContextHolder;
import gcu.common.domain.Page;
import gcu.module.rbac.dao.RoleDao;
import gcu.module.rbac.dao.UserDao;
import gcu.module.rbac.daomain.Role;
import gcu.module.rbac.daomain.User;
import gcu.shiro.ShiroKit;
import gcu.util.Pager;
import gcu.util.SFormt;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by haol on 2016/9/20.
 */
@Service
public class UserService {

      @Autowired
      private UserDao userDao;
      @Autowired
      private RoleDao roleDao;

      public User queryUserByName(String username) {
            return userDao.queryUserByName(username);
      }

      /*获取全部角色*/
      public List<User> queryUserList(User user, Pager pager) {
            return userDao.queryUserList(user, pager);
      }
      public List<User> queryUserList() {
            return userDao.queryUserList();
      }
      public void add(User user) {
            user.setCreateTime(SFormt.NOWDATE_y_M_dhms());
            user.setPassword(ShiroKit.md5Pwd(user.getPassword(), user.getUserName()));
            userDao.addUser(user);

            Integer roleId = Integer.parseInt( user.getRoleItem());
            user = userDao.queryUserByName(user.getUserName());
            roleDao.addUserRole(user.getId(),roleId);

      }

      public void update(User user) {
            userDao.updateUser(user);
            Integer roleId = Integer.parseInt( user.getRoleItem());
            roleDao.updateUserRole(user.getId(),roleId);
      }

      public void delete(int id) {
            userDao.deleteUser(id);
      }
      public void updatePwd(String username,String password) {
            String mdPwd = ShiroKit.md5Pwd(password,username);
            userDao.updatePwd(username,mdPwd);
      }

      public User queryUserById(Integer id) {
            return userDao.queryUserById(id);
      }

      public void updatTime(String time,String username) {
            userDao.updateTime(time,username);
      }

}
